package com.itcast.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itcast.dao.CheckItemMapper;
import com.itcast.health.pojo.CheckItem;
import com.itcast.health.pojo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.List;
import java.util.Set;

/**
 * @author Gao
 * @version v1.0
 * @date 2019/11/16 11:52
 * @description  检查项的服务实现
 **/
@Service(interfaceClass = CheckItemService.class,loadbalance = "random")
@Transactional
public class CheckItemServiceImpl implements CheckItemService {
    @Autowired
    private CheckItemMapper checkItemMapper;
    @Autowired
    private JedisPool jedisPool;
    /**
     * 向checkitem表插入数据
     * @param checkItem
     */
    @Override
    public void add(CheckItem checkItem) {
        checkItemMapper.add(checkItem);

        Jedis resource = jedisPool.getResource();
        resource.del("AllSetmeal");
        Set<String> keys = resource.keys("*");
        for (String key : keys) {
            if (key.contains("Setmeal")){
                resource.del("Setmeal");
            }
        }
    }
    /**
     * 查询分页数据
     * @param currentPage  当前页码
     * @param pageSize      显示条数
     * @param queryString   查询条件
     * @return
     */
    @Override
    public PageResult findPage(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage,pageSize);
        Page<CheckItem>page = checkItemMapper.findPage(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }


    /**
     * 根据id删除对应的检查项
     *
     * @param id
     */
    @Override
    public void delete(Integer id) {
        //查询当前检查项是否和检查组关联
        long count=checkItemMapper.findCountByCheckItemId(id);
        if (count>0){
            throw new RuntimeException("当前检查项被引用,无法删除");
        }
        //删除对应检查项
        checkItemMapper.deleteCheckItemById(id);

        Jedis resource = jedisPool.getResource();
        resource.del("AllSetmeal");
        Set<String> keys = resource.keys("*");
        for (String key : keys) {
            if (key.contains("Setmeal")){
                resource.del("Setmeal");
            }
        }
    }

    /**
     * 根据id修改对应的检查项
     * @param checkItem
     */
    @Override
    public void editCheckItemById(CheckItem checkItem) {
        checkItemMapper.editCheckItemById(checkItem);

        Jedis resource = jedisPool.getResource();
        resource.del("AllSetmeal");
        Set<String> keys = resource.keys("*");
        for (String key : keys) {
            if (key.contains("Setmeal")){
                resource.del("Setmeal");
            }
        }
    }

    /**
     * 根据id查询对应的检查项
     * @param id
     * @return
     */
    @Override
    public CheckItem findCheckItemById(Integer id) {
        return checkItemMapper.findCheckItemById(id);
    }

    /**
     * 查询所有的检查项信息
     *
     * @return
     */
    @Override
    public List<CheckItem> findAll() {
        return checkItemMapper.finAll();
    }
}